package com.smartstate.iot.service.impl;

import com.alibaba.excel.EasyExcel;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.mapper.TopicLogMapper;
import com.smartstate.iot.mapper.TopicMapper;
import com.smartstate.iot.service.TopicService;
import com.smartstate.iot.vo.TopicUpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class TopicImpl extends ServiceImpl<TopicMapper, TopicConfig> implements TopicService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private TopicLogMapper topicLogMapper;


    /**
     * 查询redis中存在多少个设备(key)
     *
     * @return
     */


    @Override
    public Integer findAllDevice() {
        ArrayList<String> device = getKeys();
        int size = device.size();
        return size;
    }

    /**
     * 判断这个deviceId 是否已经存在
     * @param deviceId
     * @return
     */
    @Override
    public Boolean logOnly(String deviceId) {
        Integer deviceOfOne = topicMapper.getDeviceOfOne(deviceId);
        if (deviceOfOne > 0){
            return false;
        }
        return true;
    }

    /**
     * 更新盒子心跳信息
     */
    @Override
    public Integer updateConfig(TopicConfig topicConfig) {
        return topicMapper.updateByPortIp(topicConfig);
    }

    /**
     * 查询这个当前端口的上次心跳信息时间戳
     */
    @Override
    public TopicConfig getUpTime(TopicConfig topicConfig) {
        TopicConfig upTime = topicMapper.getUpTime(topicConfig);
        return upTime;
    }

    @Override
    public List<TopicConfig> getNormalBoxes() {
        return topicMapper.getNormalBoxes();
    }

    /**
     * 查询网管设备总数
     */
    @Override
    public List<Integer> getAllType() {
        ArrayList<Integer> allTypeList = new ArrayList<>();
        LinkedList<String> typeList = new LinkedList<>();
        List<String> allType = topicMapper.getAllType();
        int size = allType.size();
        for (String s : allType) {
            String typeSize = topicLogMapper.getTypeSize(s);
            typeList.add(typeSize);
        }
        int ok = 0;
        int off = 0;
        int on = 0;
        for (String s : typeList) {
            if ("正常".equals(s)) {
                ok++;
            } else if ("异常".equals(s)) {
                off++;
            } else {
                on++;
            }
        }
        allTypeList.add(ok);
        allTypeList.add(on);
        allTypeList.add(off);
        allTypeList.add(size);
        return allTypeList;
    }

    @Override
    public String getTimesTamps(String deviceId) {
        return topicLogMapper.getTimesTamps(deviceId);
    }


    /**
     * 获取redis中所有的key
     *
     * @return
     */
    private ArrayList<String> getKeys() {
        List<TopicLog> topicLogs = topicLogMapper.getAllDevice();
        TreeSet<String> boxName = new TreeSet<>();
        for (TopicLog topicLog : topicLogs) {
            boxName.add(topicLog.getDeviceId());
        }
        System.out.println("boxName = " + boxName);
        //  获取所有的key
        ArrayList<String> keys = new ArrayList<>();
        for (String logSet : boxName) {
            Set<String> tempKeys = redisTemplate.keys(logSet + ":*");
            if (tempKeys != null) {
                keys.addAll(tempKeys);
            }
        }
        System.out.println("keys = " + keys);
        return keys;
    }


}
